const _Mongo = require('../../../Mongo')
const _cmsMongo = new _Mongo('mongodb://192.168.1.2:27017', 'cms')

async function getMenuTree() {
	let moduleCol = await _cmsMongo.getCollection('module')
	let moduleList = await moduleCol.find({}, {
		create_time: 0,
		modify_time: 0,
	}).sort({
		create_time: 1,
	}).toArray()
	let moduleMap = {}
	let treeData = []
	for (let i = 0; i < moduleList.length; i++) {
		let module = moduleList[i]
		if (module.parent_id == 0)
			treeData.push(module)
		else
			moduleMap[module.module_id] = module
	}
	for (let i = 0; i < treeData.length; i++) {
		let module = treeData[i]
		addChildren(module, moduleMap)
	}
	console.log('---treeData---', JSON.stringify(treeData))
	// console.log('---moduleMap---', JSON.stringify(moduleMap))

}

function addChildren(module, moduleMap) {
	if (!module || !Array.isArray(module.children))
		return
	// console.log('-------module------', module)
	let children = module.children
	// console.log('-------children------', children)
	module.children = []
	for (let i = 0; i < children.length; i++) {
		let cmId = children[i]
		let m = moduleMap[cmId]
		if (m) {
			module.children.push(m)
			if (Array.isArray(m.children))
				addChildren(m, moduleMap)
		}
	}
}

getMenuTree().then(r => {

})